/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Validar_Sistema.java
 *
 * Created on 15/06/2009, 06:44:14 PM
 */

package General;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.jgoodies.looks.windows.*;
import javax.swing.ImageIcon;
import Control.Conexion;
import javax.swing.JOptionPane;
/**
 *
 * @author GAMA
 */
public class Validar_Sistema extends javax.swing.JFrame {

    /** Creates new form Validar_Sistema */
    public Validar_Sistema() {
        initComponents();
        //setIconImage(new ImageIcon(this.getClass().getResource("/imagenes/llave16.gif")).getImage());
    }

    public void capturar_datos_validar(){
        String usuario=txtusuario.getText();
        String clave=new String (txtcontrasenia.getPassword());
        System.out.println("usu:"+usuario);
        System.out.println("cla:"+clave);
        Conexion c=new Conexion();
        int tipo_usuario=c.validar_en_sistema(usuario, clave);
        String datos_usuario=c.obtener_datos_usuario(usuario, clave);
        int codigo_usuario=c.retornar_codigo_usuario(usuario);
        switch(tipo_usuario){
            case 1: this.setVisible(false);
                    Administrador p=new Administrador(this,datos_usuario, codigo_usuario);
                    p.setVisible(true);
                    break;
            case 2: this.setVisible(false);
                    Operario o=new Operario(this,datos_usuario,codigo_usuario);
                    o.setVisible(true);
                    break;
            case 3: this.setVisible(false);
                    Vendedor v=new Vendedor(this,datos_usuario,codigo_usuario);
                    v.setVisible(true);
                    break;
            default:  JOptionPane.showMessageDialog(this,"Usuario o contraseña inválida","Error",JOptionPane.ERROR_MESSAGE);
                     limpiar_campos();

            break;
        }

    }
    public void limpiar_campos(){
         txtusuario.setText("");
                      txtcontrasenia.setText("");
                      txtusuario.requestFocus();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtusuario = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtcontrasenia = new javax.swing.JPasswordField();
        jPanel3 = new javax.swing.JPanel();
        btnaceptar = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        btncancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Acceso al Sistema");
        setIconImage(new ImageIcon(this.getClass().getResource("/imagenes/llave16.gif")).getImage());
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 20));
        jLabel1.setForeground(new java.awt.Color(51, 0, 153));
        jLabel1.setText("BIENVENIDOS AL ");
        jPanel1.add(jLabel1);

        jPanel2.add(jPanel1);

        jLabel3.setFont(new java.awt.Font("Calibri", 1, 20));
        jLabel3.setForeground(new java.awt.Color(51, 0, 153));
        jLabel3.setText("SISTEMA DE CONTROL DE INVENTARIOS ");
        jPanel5.add(jLabel3);

        jPanel2.add(jPanel5);

        jPanel6.setMinimumSize(new java.awt.Dimension(122, 50));
        jPanel6.setPreferredSize(new java.awt.Dimension(122, 50));

        jLabel2.setFont(new java.awt.Font("Calibri", 3, 22));
        jLabel2.setForeground(new java.awt.Color(51, 0, 204));
        jLabel2.setText("ATSISCOIN");
        jLabel2.setPreferredSize(new java.awt.Dimension(120, 27));
        jPanel6.add(jLabel2);

        jPanel2.add(jPanel6);

        jPanel7.setMinimumSize(new java.awt.Dimension(75, 40));
        jPanel7.setPreferredSize(new java.awt.Dimension(214, 40));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Usuario: ");
        jLabel4.setPreferredSize(new java.awt.Dimension(79, 17));
        jPanel7.add(jLabel4);

        txtusuario.setPreferredSize(new java.awt.Dimension(120, 20));
        jPanel7.add(txtusuario);

        jPanel2.add(jPanel7);

        jPanel4.setMinimumSize(new java.awt.Dimension(100, 50));
        jPanel4.setPreferredSize(new java.awt.Dimension(515, 40));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Contraseña: ");
        jPanel4.add(jLabel5);

        txtcontrasenia.setPreferredSize(new java.awt.Dimension(120, 20));
        txtcontrasenia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcontraseniaActionPerformed(evt);
            }
        });
        jPanel4.add(txtcontrasenia);

        jPanel2.add(jPanel4);

        jPanel3.setMinimumSize(new java.awt.Dimension(100, 40));
        jPanel3.setPreferredSize(new java.awt.Dimension(224, 50));

        btnaceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Aceptar16.gif"))); // NOI18N
        btnaceptar.setText("Aceptar");
        btnaceptar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnaceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaceptarActionPerformed(evt);
            }
        });
        jPanel3.add(btnaceptar);

        jPanel8.setPreferredSize(new java.awt.Dimension(10, 20));

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        jPanel3.add(jPanel8);

        btncancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Cancelar16.gif"))); // NOI18N
        btncancelar.setText("Cancelar");
        btncancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });
        jPanel3.add(btncancelar);

        jPanel2.add(jPanel3);

        getContentPane().add(jPanel2, new java.awt.GridBagConstraints());

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-408)/2, (screenSize.height-334)/2, 408, 334);
    }// </editor-fold>//GEN-END:initComponents

    private void txtcontraseniaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcontraseniaActionPerformed
        // TODO add your handling code here:
        this.repaint();
        this.btnaceptar.setSelected(true);
        capturar_datos_validar();
        this.btnaceptar.setSelected(false);
}//GEN-LAST:event_txtcontraseniaActionPerformed

    private void btnaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaceptarActionPerformed
        // TODO add your handling code here:
          this.repaint();
        this.btnaceptar.setSelected(true);
        capturar_datos_validar();
        this.btnaceptar.setSelected(false);
}//GEN-LAST:event_btnaceptarActionPerformed

    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
        // TODO add your handling code here:
        System.exit(0);
}//GEN-LAST:event_btncancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                   try {
            UIManager.setLookAndFeel(new WindowsLookAndFeel());

              new Validar_Sistema().setVisible(true);
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
               
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnaceptar;
    private javax.swing.JButton btncancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPasswordField txtcontrasenia;
    private javax.swing.JTextField txtusuario;
    // End of variables declaration//GEN-END:variables

}
